package me.goldze.mvvmhabit.http;

import android.util.Log;

import com.google.gson.JsonParseException;

import org.apache.http.conn.ConnectTimeoutException;
import org.json.JSONException;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.util.concurrent.TimeoutException;

import javax.net.ssl.SSLException;

import me.goldze.mvvmhabit.http.interceptor.logging.LocalLogUtil;
import me.goldze.mvvmhabit.utils.KLog;
import retrofit2.HttpException;

public class ExceptionHandle {

    private static final int IS_REGISTER = 400;
    private static final int UNAUTHORIZED = 401;
    private static final int FORBIDDEN = 403;
    private static final int SERVER_UNKNOWN = 405;
    private static final int NOT_FOUND = 404;
    private static final int REQUEST_TIMEOUT = 408;
    private static final int INTERNAL_SERVER_ERROR = 500;
    private static final int BAD_GATEWAY = 502;
    private static final int SERVICE_UNAVAILABLE = 503;
    private static final int GATEWAY_TIMEOUT = 504;

    public static ResponeThrowable handleException(Throwable e) {
        ResponeThrowable ex;
        Log.i("tag", "e.toString = " + e.toString());
        if (e instanceof HttpException) {
            HttpException httpException = (HttpException) e;
            ex = new ResponeThrowable(e, UNAUTHORIZED);
            switch (httpException.code()) {
                case UNAUTHORIZED:
                case FORBIDDEN:
                case NOT_FOUND:
                case REQUEST_TIMEOUT:
                case GATEWAY_TIMEOUT:
                case INTERNAL_SERVER_ERROR:
                case BAD_GATEWAY:
                case SERVICE_UNAVAILABLE:
                case SERVER_UNKNOWN:
                default:
                    //ex.code = httpException.code();
                    ex.message = "网络错误:" + httpException.code();
                    break;
            }
        } else if (e instanceof SocketTimeoutException| e instanceof TimeoutException| e instanceof ConnectTimeoutException) {
            SocketTimeoutException exception = (SocketTimeoutException) e;
            ex = new ResponeThrowable(exception, ERROR.NETWORD_ERROR);
            ex.message = "当前网络不稳定" ;
        } else if (e instanceof IllegalArgumentException) {
            IllegalArgumentException exception = (IllegalArgumentException) e;
            ex = new ResponeThrowable(exception, ERROR.DATA_NULL_ERROR);
            ex.message = exception.getMessage();
        } else if (e instanceof ServerException) {
            ServerException resultException = (ServerException) e;
            ex = new ResponeThrowable(resultException, resultException.code);
            ex.message = "数据查询错误" + e.getMessage();
        } else if (e instanceof JsonParseException
                || e instanceof JSONException
            /*|| e instanceof ParseException*/) {
            ex = new ResponeThrowable(e, ERROR.PARSE_ERROR);
            ex.message = "解析错误" + e.getMessage();
        } else if (e instanceof ConnectException) {
            ex = new ResponeThrowable(e, ERROR.NETWORD_ERROR);
            ex.message = "连接失败" + e.getMessage();
        } else if (e instanceof SSLException) {
            ex = new ResponeThrowable(e, ERROR.SSL_ERROR);
            ex.message = "证书验证失败,请配置正确的服务器";
        } else if (e instanceof TimeoutException) {
            ex = new ResponeThrowable(e, ERROR.SSL_ERROR);
            ex.message = "数据处理超时，请重新获取";
        } else if (e instanceof NoDataException) {
            ex = new ResponeThrowable(e, ERROR.DATA_NULL_ERROR);
            ex.message = ((NoDataException) e).getErrorInfo();
        } else {
            ex = new ResponeThrowable(e, ERROR.UNKNOWN);
            ex.message = "网络错误";
        }
        return ex;
    }


    /**
     * 约定异常
     */
    public class ERROR {
        /**
         * 未知错误
         */
        public static final int UNKNOWN = 1000;
        /**
         * 解析错误
         */
        public static final int PARSE_ERROR = 1001;
        /**
         * 网络错误
         */
        public static final int NETWORD_ERROR = 1002;
        /**
         * 协议出错
         */
        public static final int HTTP_ERROR = 1003;

        /**
         * 证书出错
         */
        public static final int SSL_ERROR = 1005;
        /**
         * 数据空异常
         */
        public static final int DATA_NULL_ERROR = 1006;
    }

    public static class ResponeThrowable extends Exception {
        public int code;
        public String message;

        public ResponeThrowable(Throwable throwable, int code) {
            super(throwable);
            this.code = code;
        }
    }

    /**
     * ServerException发生后，将自动转换为ResponeThrowable返回
     */
    class ServerException extends RuntimeException {
        int code;
        String message;
    }

}